﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NXDO.Data;
using NXDO.Data.Security.WebSecurity;
using NXDO.WebAPI.Controllers;
using System;
using TDM.DbData.DataEntity;

namespace TDM.WebSvr.Controllers
{
    /// <summary>
    /// 用户成员资格管理接口
    /// </summary>
    public class DaUserController : ApiMemberManagerController<DaUser, long>
    {
        /// <summary>
        /// 获得主键
        /// </summary>
        /// <returns></returns>
        protected override string GetPkName()
        {
            return "UserID";
        }

        /// <summary>
        /// 获得表名
        /// </summary>
        /// <returns></returns>
        protected override string GetTableName()
        {
            return "DaUser";
        }

        /// <summary>
        /// 新增用户
        /// 创建时间创建人在服务端赋值
        /// </summary>
        /// <param name="data">用户数据</param>
        /// <returns></returns>
        public override DaUser Post([FromBody] DaUser data)
        {
            ////密码加密
            //var encryp = System.Configuration.ConfigurationManager.AppSettings["PwdEncryption"];
            //bool pwdEncryp = Convert.ToBoolean(encryp);
            //if (pwdEncryp)
            //{
            //    var passWord = data.Password;
            //    var bytes = Encoding.UTF8.GetBytes(passWord);
            //    var keyBytes = Encoding.UTF8.GetBytes("shenbo");
            //    // HMACSHA512
            //    var hsha512Bytes = HMACSHA512.HashData(keyBytes, bytes);
            //    var hsha512Str = Convert.ToBase64String(hsha512Bytes);
            //    data.Password = hsha512Str;
            //}
            //服务端赋值创建时间
            //var user = HttpContext.User as NXDO.Data.Security.Principal.MemberPrincipal;
            //long uid = Convert.ToInt64(user?.MemberId);
            //if (uid == 0) throw new Exception("未获得登录信息");
            if (Entity.Exists<DaUser>(u => u.UserName == data.UserName)) throw new Exception("该用户已存在");
            //data.CreateUID = uid;
            data.IsEnable = false;
            data.CreateTime = DateTime.Now;
            return base.Post(data);
        }

        [AllowAnonymous]
        public override (MembershipCreateStatus status, string error, MembershipUser member, DaUser user) CreateUser([FromBody] DaUser user)
        {

            return base.CreateUser(user);
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public override DaUser Put([FromBody] DaUser data)
        {
            ////密码加密
            //var encryp = System.Configuration.ConfigurationManager.AppSettings["PwdEncryption"];
            //bool pwdEncryp = Convert.ToBoolean(encryp);
            //if (pwdEncryp)
            //{
            //    var passWord = data.Password;
            //    var bytes = Encoding.UTF8.GetBytes(passWord);
            //    var keyBytes = Encoding.UTF8.GetBytes("shenbo");
            //    // HMACSHA512
            //    var hsha512Bytes = HMACSHA512.HashData(keyBytes, bytes);
            //    var hsha512Str = Convert.ToBase64String(hsha512Bytes);
            //    data.Password = hsha512Str;
            //}
            if (Entity.Exists<DaUser>(u => u.UserName == data.UserName && u.UserID != data.UserID)) throw new Exception("该用户已存在");
            return base.Put(data);
        }

    }
}
